<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006, 2007 Russell Peter Phillips

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

//Export bookings list to csv
include ('../inc/inc_head_db.php');
include ('../inc/inc_admin.php');

//Send headers to tell browser that this is a CSV file
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=bookings.csv;");

//Get list of players marked as paid
$key = CRYPT_KEY;
$db_prefix = DB_PREFIX;
$sql = "SELECT plPlayerID, " . 
	"plFirstName, " .
	"plSurname, " .
	"AES_DECRYPT(pleAddress1, '$key') AS dAddress1, " .
	"AES_DECRYPT(pleAddress2, '$key') AS dAddress2, " .
	"AES_DECRYPT(pleAddress3, '$key') AS dAddress3, " .
	"AES_DECRYPT(pleAddress4, '$key') AS dAddress4, " .
	"AES_DECRYPT(plePostcode, '$key') AS dPostcode, " .
	"AES_DECRYPT(pleTelephone, '$key') AS dTelephone, " .
	"AES_DECRYPT(pleMobile, '$key') AS dMobile, " .
	"plEmail, " .
	"plDOB, " .
	"AES_DECRYPT(pleMedicalInfo, '$key') AS dMedicalInfo, " .
	"plEmergencyName, " .
	"AES_DECRYPT(pleEmergencyNumber, '$key') AS dEmergencyNumber, " .
	"plEmergencyRelationship, " .
	"plDietary, " .
	"plBookAs, " .
	"plBunkAssigned, " .
	"plNotes, " .
	"plAdminNotes, " .
	"chName, chRace, chCulture, chReason, " .
	"bkDatePaymentConfirmed, bkMealTicket, bkPayOnGate " .
	"FROM {$db_prefix}players, {$db_prefix}characters, {$db_prefix}bookings " .
	"WHERE plPlayerID = chPlayerID AND chPlayerID = bkPlayerID AND bkDatePaymentConfirmed <> '0000-00-00' " .
	"ORDER BY plBookAs, plSurname";
$result = ba_db_query ($link, $sql);

//Header row
echo '"Player ID","Player First Name","Player Surname",';
echo '"Address 1","Address 2","Address 3","Address 4","Postcode","Telephone","Mobile","email","dob","NOK","NOK contact",';
echo '"NOK Relationship","Medical","Medical details","Diet","Booking As","Bunk","Meal Ticket","Paying on Gate","OOC Notes","Admin Notes",';
echo '"Character name","Race","Culture","Reason for Attending",';
echo '"Date Paid"' . "\n";

while ($row = ba_db_fetch_assoc ($result)) {
	echo '"' . PID_PREFIX . sprintf ('%03s', $row ['plPlayerID']) . '",';
	echo '"' . stripslashes ($row ['plFirstName']) . '",';
	echo '"' . stripslashes ($row ['plSurname']) . '",';
	echo '"' . stripslashes ($row ['dAddress1']) . '",';
	echo '"' . stripslashes ($row ['dAddress2']) . '",';
	echo '"' . stripslashes ($row ['dAddress3']) . '",';
	echo '"' . stripslashes ($row ['dAddress4']) . '",';
	echo '"' . stripslashes ($row ['dPostcode']) . '",';
	echo '". ' . stripslashes ($row ['dTelephone']) . ' .",';
	echo '". ' . stripslashes ($row ['dMobile']) . ' .",';
	echo '"' . stripslashes ($row ['plEmail']) . '",';
	$dDOB = stripslashes ($row ['plDOB']);
	$iYear = substr ($dDOB, 0, 4);
	$iMonth = substr ($dDOB, 4, 2);
	$iDate = substr ($dDOB, 6, 2);
	$sDOB = "$iDate-$iMonth-$iYear";
	echo '"' . $sDOB . '",';
	echo '"' . stripslashes ($row ['plEmergencyName']) . '",';
	echo '". ' . stripslashes ($row ['dEmergencyNumber']) . ' .",';
	echo '"' . stripslashes ($row ['plEmergencyRelationship']) . '",';
	if ($row ['dMedicalInfo'] != '')
		echo '"MED",';
	else
		echo '"",';
	echo '"' . stripslashes ($row ['dMedicalInfo']) . '",';
	echo '"' . stripslashes ($row ['plDietary']) . '",';
	echo '"' . stripslashes ($row ['plBookAs']) . '",';
	if ($row ['plBunkAssigned'] == 0)
		echo '"No",';
	else
		echo '"Yes",';
	if ($row ['bkMealTicket'] == 0)
		echo '"No",';
	else
		echo '"Yes",';
	if ($row ['bkPayOnGate'] == 0)
		echo '"No",';
	else
		echo '"Yes",';
	echo '"' . stripslashes ($row ['plNotes']) . '",';
	echo '"' . stripslashes ($row ['plAdminNotes']) . '",';
	echo '"' . stripslashes ($row ['chName']) . '",';
	echo '"' . stripslashes ($row ['chRace']) . '",';
	echo '"' . stripslashes ($row ['chCulture']) . '",';
	echo '"' . stripslashes ($row ['chReason']) . '",';
	//Date payment received
	$dPaid = stripslashes ($row ['bkDatePaymentConfirmed']);
	$iYear = substr ($dPaid, 0, 4);
	$iMonth = substr ($dPaid, 5, 2);
	$iDate = substr ($dPaid, 8, 2);
	$sPaid = "$iDate-$iMonth-$iYear";
	echo '"' . $sPaid . '"';
	echo "\n";
}
?>
